---
title: Flatten form fields
summary: Bakes AcroForm field values into page content, removing interactivity.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-17
library: PdfAPI.Core
reference_type: package
operation_id: OP-07
trigger: PdfClientService.Flatten(byte[]) invoked
---

# Flatten form fields

This operation removes interactive AcroForm fields from a PDF by writing their current values into page content via an iTextSharp `PdfStamper` with form flattening enabled.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-07 |
| **Trigger** | Caller invokes `Flatten(byte[] file)` |
| **Inputs** | PDF bytes |
| **Outputs** | Flattened PDF bytes, or the original bytes unchanged |
| **Primary components** | `PdfAPI.Core.PdfClientService.Flatten` |
| **Entry point** | `PdfAPI.Core\Services\PdfClientService.cs` |

## Steps

1. Run [Validate PDF bytes](op-01-validate-pdf-bytes.md) to guard the input. ([BR-01](../business-rules.md#br-01-pdf-signature-validation), [BR-02](../business-rules.md#br-02-null-bytes-are-never-a-pdf), [BR-03](../business-rules.md#br-03-invalid-pdf-input-aborts-with-pdfexception))
2. Open a `PdfReader` over the bytes.
3. If `reader.AcroForm` is null, return the input bytes unchanged. ([BR-11](../business-rules.md#br-11-flattening-only-applies-with-acroform))
4. Otherwise write through a `PdfStamper` with `FormFlattening = true` into a `MemoryStream`, close the stamper, and return the stream bytes. ([BR-11](../business-rules.md#br-11-flattening-only-applies-with-acroform))
   - The `MemoryStream` in this step is not disposed and the `PdfStamper` is closed rather than disposed.*

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start(["Flatten(bytes)"]) --> Guard["OP-01: ValidatePdf"]
	Guard --> Open["Open PdfReader"]
	Open --> Form{reader.AcroForm null?}
	Form -->|Yes| Unchanged([Return original bytes])
	Form -->|No| Stamp["PdfStamper with<br>FormFlattening = true"]
	Stamp --> Return([Return flattened bytes])
```

## Failure and edge-case behavior
- A PDF without an AcroForm is returned byte-for-byte unchanged rather than being reprocessed ([BR-11](../business-rules.md#br-11-flattening-only-applies-with-acroform)).

> **Note:** Statements marked with `*` were inferred from code structure, naming
> conventions, or contextual evidence where direct confirmation was not available
> in source artifacts.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [Security and authorization](../security-authorization.md)

## Sources
- `PdfAPI.Core\Services\PdfClientService.cs`
